import { Controller, Post, Delete, UseGuards, Request, Body, Param, Get, UseInterceptors, CacheInterceptor, CacheTTL, Query } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { GalleryService } from './gallery.service';
import { ApiParam, ApiTags } from '@nestjs/swagger';

@ApiTags('gallery')
@Controller('gallery')
export class GalleryController {
    constructor(private readonly galleryService: GalleryService) { }

    @UseGuards(AuthGuard('jwt'))
    @Post('/create')
    AddCollection(@Request() req, @Body() post_json) {
        return this.galleryService.create(req.user, post_json)
    }

    @UseGuards(AuthGuard('jwt'))
    @Delete('/delete/:gid')
    DeleteDoc(@Param() params) {
        return this.galleryService.deleteDoc(params)
    }

    @UseGuards(AuthGuard('jwt'))
    @Post('/addTag')
    AddTag(@Request() req, @Body() post_json) {
        return this.galleryService.addTag(req.user, post_json)
    }

    @UseGuards(AuthGuard('jwt'))
    @Post('/niceJob')
    NiceJob(@Query() params, @Request() req) {
        return this.galleryService.niceJob(req.user.userId, params.gid)
    }

    @UseGuards(AuthGuard('jwt'))
    @Post('/badJob')
    BadJob(@Query() params, @Request() req) {
        return this.galleryService.badJob(req.user.userId, params.gid)
    }

    @UseGuards(AuthGuard('jwt'))
    @Get('/niceCount')
    // @UseInterceptors(CacheInterceptor)
    // @CacheTTL(1)
    async NiceCount(@Query() params) {
        return await this.galleryService.niceCount(params.gid)
    }

    @UseGuards(AuthGuard('jwt'))
    @Post('/searchTag')
    searchTag(@Body() post_json) {
        return this.galleryService.searchTag(post_json)
    }

    @UseGuards(AuthGuard('jwt'))
    @Post('/searchBody')
    searchBody(@Body() post_json) {
        return this.galleryService.searchBody(post_json)
    }

    // @UseGuards(AuthGuard('jwt'))
    @UseInterceptors(CacheInterceptor)
    @Get('/view/:gid')
    Viewposts(@Param() post_json) {
        return this.galleryService.viewPosts(post_json.gid, null, true)
    }

    // @UseGuards(AuthGuard('jwt'))
    // @UseInterceptors(CacheInterceptor)
    // @Get('/view/:gid')
    // SetToken(@Param() params, @Body() token_json) {
    //     return this.galleryService.setToken(params.gid, token_json)
    // }

    @UseInterceptors(CacheInterceptor)
    @Post('/view/:gid')
    ViewpostsWithToken(@Param() post_json, @Body() token_json) {
        return this.galleryService.viewPosts(post_json.gid, token_json, false)
    }

    @UseGuards(AuthGuard('jwt'))
    @UseInterceptors(CacheInterceptor)
    @CacheTTL(15) //Cache TTL can be set here
    @Get('/p/:pagenum')
    async page(@Param() param, @Request() req) {
        return this.galleryService.page(param.pagenum, req.user)
    }
    @UseGuards(AuthGuard('jwt'))
    @UseInterceptors(CacheInterceptor)
    @CacheTTL(10) //Cache TTL can be set here
    @Get('/count')
    async count() {
        return this.galleryService.count()
    }
}
